import router from './router'
import { useUserStore } from '@/stores/user'
// 免登录白名单
const whiteList = ['/login']

const loginPath = '/login'
const defaultPath = '/home'

router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()

  if (userStore.token) {
    if (to.path === loginPath) { 
      next({ path: defaultPath })
    } else {
      next()
    }
  } else {
    // 如果用户没有token且不在白名单中，才重定向到登录页
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next({ path: loginPath })
    }
  }
})
